﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace jnxc.Web.addHappen
{
    /// <summary>
    /// HappenMAN 的摘要说明
    /// </summary>
    public class HappenMAN : IHttpHandler, IRequiresSessionState
    {
        jnxc.BLL.addhappen bll = new BLL.addhappen();
        StringBuilder result = new StringBuilder();
        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
            {
                Query(context);
            }
            else if (ac == "add")
            {
                Add(context);
            }
            else if (ac == "delete")
            {
                Delete(context);
            }
            else if (ac == "modify")
            {
                Modify(context);
            }
            else if (ac == "querymy")
            {
                Querymy(context);
            }
            else if (ac == "queryt")
            {
                Queryt(context);   
            }
               
           
        }

        public void Add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                DateTime?  dctime = null;
                int aid = 0, dpid = 0,nd=0,yf=0;
                string generation= "";
                decimal yjarea = 0, jzarea = 0, jcarea = 0, dckdgarea = 0, fbarea = 0, jcfgarea = 0, generationfsarea = 0,
                    ljqd = 0, ljzd = 0, ljzzd = 0, fshj = 0, fsqd = 0, fszd = 0, fszzd = 0, tbcxfs = 0, fsl = 0, yjczarea = 0;
                string temp = "";
                temp = context.Request.Form["aid"];
                if (temp != "") aid = int.Parse(temp);
                string trees = context.Request.Form["trees"];
               
                temp = context.Request.Form["yjarea"];
                if (temp != "") yjarea = decimal.Parse(temp);
                temp = context.Request.Form["dpid"];
                if (temp != "") dpid = int.Parse(temp);
                temp = context.Request.Form["jzarea"];
                if (temp != "") jzarea = decimal.Parse(temp);
                temp = context.Request.Form["jcarea"];
                if (temp != "") jcarea = decimal.Parse(temp);
                temp = context.Request.Form["generation"];
                if (temp != "") generation = temp;
                temp = context.Request.Form["dctime"];
                if (temp != "") dctime = DateTime.Parse(temp);
                temp = context.Request.Form["dckdgarea"];
                if (temp != "") dckdgarea = decimal.Parse(temp);
                temp = context.Request.Form["fbarea"];
                if (temp != "") fbarea = decimal.Parse(temp);
                temp = context.Request.Form["jcfgarea"];
                if (temp != "") jcfgarea = decimal.Parse(temp);
                temp = context.Request.Form["generationfsarea"];
                if (temp != "") generationfsarea = decimal.Parse(temp);
                temp = context.Request.Form["ljqd"];
                if (temp != "") ljqd = decimal.Parse(temp);
                temp = context.Request.Form["ljzd"];
                if (temp != "") ljzd = decimal.Parse(temp);
                temp = context.Request.Form["ljzzd"];
                if (temp != "") ljzzd = decimal.Parse(temp);
                temp = context.Request.Form["fshj"];
                if (temp != "") fshj = decimal.Parse(temp);
                temp = context.Request.Form["fsqd"];
                if (temp != "") fsqd = decimal.Parse(temp);
                temp = context.Request.Form["fszd"];
                if (temp != "") fszd = decimal.Parse(temp);
                temp = context.Request.Form["fszzd"];
                if (temp != "") fszzd = decimal.Parse(temp);
                temp = context.Request.Form["tbcxfs"];
                if (temp != "") tbcxfs = decimal.Parse(temp);
                temp = context.Request.Form["fsl"];
                if (temp != "") fsl = decimal.Parse(temp);
                temp = context.Request.Form["yjczarea"];
                if (temp != "") yjczarea = decimal.Parse(temp);
               
                if (dctime != null)
                {
                    nd = dctime.Value.Year;
                    yf = dctime.Value.Month;
                }

                jnxc.Model.addhappen model = new jnxc.Model.addhappen();
                model.aid = aid;
                model.dpid = dpid;
                model.trees = trees;
                model.dckdgbarea = dckdgarea;
                model.jzarea = jzarea;
                model.jcarea = jcarea;
                model.generation = generation;
                model.dctime = dctime;
                model.fbarea = fbarea;
                model.ljqd = ljqd;
                model.ljzd = ljzd;
                model.ljzzd = ljzzd;
                model.fsqd = fsqd;
                model.fszd = fszd;
                model.fszzd = fszzd;
                model.fshj = fshj;
                model.fsl = fsl;
                model.generationfsarea = generationfsarea;
                model.jcfgarea = jcfgarea;
                model.tbcxfs = tbcxfs;
                model.yjarea = yjarea;
                model.yjczarea = yjczarea;
                model.nd = nd;
                model.yf = yf;
                model.uid = Web.Admin.myinfo.getID(context);   
                int rid = bll.Add(model);
                if (rid > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void Delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["addhappen_id"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();

            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();

            }

        }
        public void Modify(HttpContext context)
        {
            if (Web.Admin.myinfo.isLogin(context))
            {
                context.Response.ContentType = "text/plain";
                string ap = context.Request.Form["addhappen_id"];
                int addhappen_id = 0;
                if (ap != "") addhappen_id = int.Parse(ap);

                DateTime? dctime = null;
                int aid = 0, dpid = 0, nd = 0, yf = 0;
                string generation = "";
                decimal yjarea = 0, jzarea = 0, jcarea = 0, dckdgarea = 0, fbarea = 0, jcfgarea = 0, generationfsarea = 0,
                    ljqd = 0, ljzd = 0, ljzzd = 0, fshj = 0, fsqd = 0, fszd = 0, fszzd = 0, tbcxfs = 0, fsl = 0, yjczarea = 0;
                string temp = "";
                temp = context.Request.Form["aid"];
                if (temp != "") aid = int.Parse(temp);
                string trees = context.Request.Form["trees"];

                temp = context.Request.Form["yjarea"];
                if (temp != "") yjarea = decimal.Parse(temp);
                temp = context.Request.Form["dpid"];
                if (temp != "") dpid = int.Parse(temp);
                temp = context.Request.Form["jzarea"];
                if (temp != "") jzarea = decimal.Parse(temp);
                temp = context.Request.Form["jcarea"];
                if (temp != "") jcarea = decimal.Parse(temp);
                temp = context.Request.Form["generation"];
                if (temp != "") generation = temp.Trim();
                temp = context.Request.Form["dctime"];
                if (temp != "") dctime = DateTime.Parse(temp);
                temp = context.Request.Form["dckdgarea"];
                if (temp != "") dckdgarea = decimal.Parse(temp);
                temp = context.Request.Form["fbarea"];
                if (temp != "") fbarea = decimal.Parse(temp);
                temp = context.Request.Form["jcfgarea"];
                if (temp != "") jcfgarea = decimal.Parse(temp);
                temp = context.Request.Form["generationfsarea"];
                if (temp != "") generationfsarea = decimal.Parse(temp);
                temp = context.Request.Form["ljqd"];
                if (temp != "") ljqd = decimal.Parse(temp);
                temp = context.Request.Form["ljzd"];
                if (temp != "") ljzd = decimal.Parse(temp);
                temp = context.Request.Form["ljzzd"];
                if (temp != "") ljzzd = decimal.Parse(temp);
                temp = context.Request.Form["fshj"];
                if (temp != "") fshj = decimal.Parse(temp);
                temp = context.Request.Form["fsqd"];
                if (temp != "") fsqd = decimal.Parse(temp);
                temp = context.Request.Form["fszd"];
                if (temp != "") fszd = decimal.Parse(temp);
                temp = context.Request.Form["fszzd"];
                if (temp != "") fszzd = decimal.Parse(temp);
                temp = context.Request.Form["tbcxfs"];
                if (temp != "") tbcxfs = decimal.Parse(temp);
                temp = context.Request.Form["fsl"];
                if (temp != "") fsl = decimal.Parse(temp);
                temp = context.Request.Form["yjczarea"];
                if (temp != "") yjczarea = decimal.Parse(temp);
               
                if (dctime != null)
                {
                    nd = dctime.Value.Year;
                    yf = dctime.Value.Month;
                }

                jnxc.Model.addhappen model = new jnxc.Model.addhappen();
                model.addhappen_id = addhappen_id;
                model.aid = aid;
                model.dpid = dpid;
                model.trees = trees;
                model.dckdgbarea = dckdgarea;
                model.jzarea = jzarea;
                model.jcarea = jcarea;
                model.generation = generation;
                model.dctime = dctime;
                model.fbarea = fbarea;
                model.ljqd = ljqd;
                model.ljzd = ljzd;
                model.ljzzd = ljzzd;
                model.fsqd = fsqd;
                model.fszd = fszd;
                model.fszzd = fszzd;
                model.fshj = fshj;
                model.fsl = fsl;
                model.generationfsarea = generationfsarea;
                model.jcfgarea = jcfgarea;
                model.tbcxfs = tbcxfs;
                model.yjarea = yjarea;
                model.yjczarea = yjczarea;
                model.nd = nd;
                model.yf = yf;
                model.uid = Web.Admin.myinfo.getID(context);
                bool rid = bll.Update(model);
                if (rid)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = Web.Admin.myinfo.getID(context);
                string uname, utruename, st;
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());

                }
                if (null != context.Request.Form["page"])
                {

                    page = int.Parse(context.Request.Form["page"].ToString().Trim());

                }
                if (null != context.Request.Form["sort"])
                {

                    order = context.Request.Form["sort"].ToString().Trim();

                }
                if (null != context.Request.Form["order"])
                {

                    sort = context.Request.Form["order"].ToString().Trim();

                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" uname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" utruename like '%{0}%' and ", utruename);
                }

                strWhere.AppendFormat(" uid={0} and ", uid);
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public void Querymy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "", otype = "0";
                string idstr = "";
                int id = 0;
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                if (null != context.Request.Form["otype"])
                {//获取前台传来的值  
                    otype = context.Request.Form["otype"].ToString().Trim();
                }

                if (null != context.Request.Form["idstr"])
                {
                    idstr = context.Request.Form["idstr"].ToString().Trim();
                    if (idstr != "")
                    {
                        id = int.Parse(idstr);
                    }
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" dctime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" dctime<='{0}' and ", EndTime);
                }
                if (otype == "1")
                {
                    strWhere.AppendFormat(" dpid='{0}' and ", id);

                }
                else if (otype == "2")
                {
                    strWhere.AppendFormat(" aid='{0}' and ", id);

                }
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void Queryt(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                BLL.dpinfo blldp = new BLL.dpinfo();
                DataSet ds = blldp.GetList("");

                string str = GetTreeJsonByTable(ds.Tables[0], "dpid", "dpname", "1");
                str = "{\"id\":-1,\"text\":\"分病虫浏览\",\"children\":" + str + "}";
                BLL.areainfo blla = new BLL.areainfo();
                ds = blla.GetList("");

                string strb = GetTreeJsonByTable(ds.Tables[0], "aid", "aname", "2");
                strb = "{\"id\":-2,\"text\":\"分地区浏览\",\"children\":" + strb + "}";
                string strJson = "[{\"id\":0,\"text\":\"全部\",\"children\":[" + str + "," + strb + "]}]";
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        private string GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string addattr)
        {
            result.Remove(0, result.Length);
            if (tabel.Rows.Count > 0)
            {
                result.Append("[");

                foreach (DataRow row in tabel.Rows)
                {
                    result.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"type\":\"" + addattr + "\"},");
                }
                result = result.Remove(result.Length - 1, 1);

                result.Append("]");

            }
            return result.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}